import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { Permissions, PermissionsGuard } from '@nbase/nest-mods';
import type { IPost, IPaginationResponse } from '@nbase/shared';
import { PostService } from './post.service';
import { CreatePostDto } from './dto/create-post.dto';
import { UpdatePostDto } from './dto/update-post.dto';
import { QueryPostDto } from './dto/query-post.dto';

/**
 * 岗位控制器
 */
@ApiTags('system/post')
@ApiBearerAuth('JWT-auth')
@Controller('system/post')
@UseGuards(PermissionsGuard)
export class PostController {
  constructor(private readonly postService: PostService) {}

  /**
   * 查询所有岗位（分页查询）
   */
  @ApiOperation({ summary: '获取岗位列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:post:query')
  @Get()
  findAll(@Query() query: QueryPostDto): Promise<IPaginationResponse<IPost>> {
    return this.postService.paginatePosts(query);
  }

  /**
   * 获取岗位详情
   */
  @ApiOperation({ summary: '获取岗位详情' })
  @ApiParam({ name: 'id', description: '岗位ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '岗位不存在' })
  @Permissions('system:post:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IPost> {
    return this.postService.findById(id);
  }

  /**
   * 根据编码获取岗位
   */
  @ApiOperation({ summary: '根据编码获取岗位' })
  @ApiParam({ name: 'code', description: '岗位编码', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:post:query')
  @Get('code/:code')
  findByCode(@Param('code') code: string): Promise<IPost | null> {
    return this.postService.findByCode(code);
  }

  /**
   * 创建岗位
   */
  @ApiOperation({ summary: '创建岗位' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:post:create')
  @Post()
  create(@Body() createPostDto: CreatePostDto): Promise<IPost> {
    return this.postService.createPost(createPostDto);
  }

  /**
   * 更新岗位
   */
  @ApiOperation({ summary: '更新岗位' })
  @ApiParam({ name: 'id', description: '岗位ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '岗位不存在' })
  @Permissions('system:post:update')
  @Put(':id')
  update(@Param('id', ParseUUIDPipe) id: string, @Body() updatePostDto: UpdatePostDto): Promise<IPost> {
    return this.postService.updatePost(id, updatePostDto);
  }

  /**
   * 删除岗位
   */
  @ApiOperation({ summary: '删除岗位' })
  @ApiParam({ name: 'id', description: '岗位ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '岗位不存在' })
  @Permissions('system:post:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.postService.deletePost(id);
  }
}

